package com.itheima;

import java.util.HashMap;
import java.util.Map;

//投票统计
public class MapPractice {
    public static void main(String[] args) {
        Map<String,String> data = new HashMap<>();
        data.put("张三1","A");
        data.put("张三2","B");
        data.put("张三3","C");
        data.put("张三4","D");
        data.put("李四1","C");
        data.put("李四2","C");
        data.put("李四3","B");
        data.put("李四4","B");
        data.put("李四5","C");
        data.put("李四6","A");
        data.put("王五1","A");
        data.put("王五2","C");
        data.put("王五3","B");
        data.put("赵六1","B");
        data.put("赵六2","C");
        data.put("赵六3","D");
        data.put("赵六4","C");

        //调用方法统计
        count(data);
    }

    public static void count(Map<String,String> data){
        //1.创建Map集合,来存储投票结果
        //2.遍历data集合（key--value）

        Map<String,Integer> resultMap = new HashMap<>();
        data.forEach((k,v)->{
            //---3.将value（A）拿出来，当做key存储到新集合中，
            //---4.若A已经存在，那么对应A的value值+1
            //---5.若A不存在，那么A--1存储到新集合中
            //判断v是否已经在resultMap集合中存在了
            boolean containsKey = resultMap.containsKey(v);
            if (containsKey){//存在
                //将老的值取出来+1，重新存进去
                Integer oldValue = resultMap.get(v);
                resultMap.put(v,oldValue+1);
            }else {//不存在
                resultMap.put(v,1);
            }

        });
        //6.遍历新map集合，打印投票结果
        resultMap.forEach((k,v)->{
            System.out.println(k+"-->"+v);
        });

    }

}
